import { useUserStore } from '@/store/user'

/**
 * 权限判断钩子
 */
export function usePermission() {
  const userStore = useUserStore()

  /**
   * 检查是否拥有指定权限
   * @param permission 权限码
   * @returns boolean
   */
  const hasPermission = (permission: string): boolean => {
    if (!permission) return true

    const permissionList = userStore.getPermissionList()
    // console.log('当前获得到的所有权限配置：', permissionList)

    return permissionList.includes(permission)
  }

  /**
   * 检查是否拥有所有指定权限
   * @param permissions 权限码数组
   * @returns boolean
   */
  const hasAllPermissions = (permissions: string[]): boolean => {
    if (!permissions || permissions.length === 0) return true

    const permissionList = userStore.getPermissionList()
    return permissions.every((permission) => permissionList.includes(permission))
  }

  /**
   * 检查是否拥有任一指定权限
   * @param permissions 权限码数组
   * @returns boolean
   */
  const hasAnyPermission = (permissions: string[]): boolean => {
    if (!permissions || permissions.length === 0) return true

    const permissionList = userStore.getPermissionList()
    return permissions.some((permission) => permissionList.includes(permission))
  }

  return {
    hasPermission,
    hasAllPermissions,
    hasAnyPermission,
  }
}
